var mongodb = require('mongodb');
var config  = require(require('fs').existsSync('../config.js') ? '../config.js' : './config.js');
var common  = require('./common.js');
var server  = require('./server.js');

var Mine  = require('./world/mine.js').Mine;
var Rank  = require('./world/rank.js').Rank;
var TopOne2  = require('./world/rank.js').TopOne2;
var TowerRank  = require('./world/rank.js').TowerRank;
var League = require('./world/league.js').League;
var Replay = require('./world/replay.js').Replay;
var Combine = require('./world/combine.js').Combine;
var LeagueWar = require('./world/leaguewar.js').LeagueWar;
var LeagueBoss = require('./world/boss.js').LeagueBoss;
var LeagueDuplicate = require('./world/leagueduplicate.js').LeagueDuplicate;
var Activity = require('./world/activity.js').Activity;
var TowerActivityRank = require('./world/rank.js').TowerActivityRank;
var FightForceRank = require('./world/rank.js').FightForceRank;
var LevelRank = require('./world/rank.js').LevelRank;
var WorldBoss = require('./world/worldboss.js').WorldBoss;
var TowerRank2  = require('./world/rank.js').TowerRank2;
var Invite  = require('./world/user.js').Invite;
var YearBoss = require('./world/activity.js').YearBoss;
var Friend = require('./world/user.js').Friend;
var DataHolder = require('./world/dataholder.js').DataHolder;
var Insurgency = require('./world/insurgency.js').Insurgency;
var ElderSign = require('./world/eldersign.js').ElderSign;
var AssistMine = require('./world/assistmine.js').AssistMine;
//var HeroLog = require('./world/herolog.js').HeroLog;
var HoroscopefarmTree = require('./world/horoscopefarm.js').HoroscopefarmTree;
var gHoroscopefarmTree = new HoroscopefarmTree();

var worldItems = {};

(function main(){
    server.loadConf();
    server.loadDB(function(db){
        db.createCollection('plat', {}, function(err, result){
            var gDBPlat = db.collection('plat');
            gDBPlat.insert({_id:'_userid', 'ai':100000000+config.ServerId*100000}, 
                function(err, result){
            });
        });

        db.createCollection('user', {}, function(err, result){
        });

        
        db.createCollection('world', {}, function(err, result){
            global.gDBWorld = db.collection('world');

            Mine.create();
            Rank.create();
            TowerRank.create();
            League.create();
            Replay.create();
            Combine.create();
            TopOne2.create();
            LeagueWar.create();
            LeagueBoss.create();
            LeagueDuplicate.create();
            Activity.create();
            TowerActivityRank.create();
            FightForceRank.create();
            LevelRank.create();
            WorldBoss.create();
            TowerRank2.create();
            Invite.create();
            YearBoss.create();
            Friend.create();
            DataHolder.create();
            Insurgency.create();
            ElderSign.create();
            AssistMine.create();
            //HeroLog.create();
            gHoroscopefarmTree.create();

            worldItems['mine'] = 1; 
            worldItems['rank'] = 1;
            worldItems['tower_rank'] = 1;
            worldItems['league'] = 1;
            worldItems['_replay_autoid'] = 1;
            worldItems['combine'] = 1;
            worldItems['league_war'] = 1;
            worldItems['league_boss'] = 1;
            worldItems['league_duplicate'] = 1;
            worldItems['activity'] = 1;
            worldItems['topone2'] = 1;
            worldItems['tower_activity_rank'] = 1;
            worldItems['fight_force_rank'] = 1;
            worldItems['level_rank'] = 1;
            worldItems['world_boss'] = 1;
            worldItems['tower_rank2'] = 1;
            worldItems['invite'] = 1;
            worldItems['year_boss'] = 1;
            worldItems['friend'] = 1;
            worldItems['data_holder'] = 1;
            worldItems[gHoroscopefarmTree.getId()] = 1;
        });

        db.createCollection('pay', {}, function(err, result){
        });

        db.createCollection('analysis', {}, function(err, result){
            /*
            if( !err ) {
                db.ensureIndex('analysis', {uid:1, action:1, c1:1, c2:1}, 
                    function(err, result){});
            }
            */
        });

        setTimeout(function(){
            var cursor = db.collection('world').find({},{_id:1});
            cursor.each(function(err, item) {
                if( cursor.isClosed() ) {
                    if( Object.keys(worldItems).length > 0 ) {
                        console.log('error');
                        process.exit(-1);
                    }else{
                        process.exit(0);
                    }
                }else{
                    if( !item ) return;
                    delete worldItems[item._id];
                }
            });
        }, 3000);

    });
})();
